package org.pz.controller;

import lombok.extern.slf4j.Slf4j;
import org.pz.entity.MallResult;
import org.pz.entity.UserEntity;
import org.pz.mq.demo.MqDemoConfig;
import org.pz.mq.demo.MqDemoProductor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 充当生产者
 * */
@RestController
@RequestMapping("/rabbitmq/demo")
@Slf4j
public class RabbitMqDemoController {

    @Autowired
    private MqDemoProductor mqDemoProductor;

    /**
     * 普通直连交换机
     * */
    @RequestMapping("/direct/sendMsg/{msg}")
    public MallResult sendMsg(@PathVariable String msg) {
        mqDemoProductor.directSendMsg(msg);
        return MallResult.success();
    }

    /**
     * 普通扇形交换机
     * */
    @RequestMapping("/fanout/sendMsg/{msg}")
    public MallResult fanoutSendMsg(@PathVariable String msg) {
        mqDemoProductor.fanoutSendMsg(msg);
        return MallResult.success();
    }

    /**
     * 序列化
     * */
    @RequestMapping("/serial/sendMsg")
    public MallResult serialSendMsg(@RequestBody UserEntity user) {
        mqDemoProductor.sendMsg(user);
        return MallResult.success();
    }

    /**
     * 发布确认-确认模式
     * 模拟到达交换机失败的情况
     * */
    @RequestMapping("/confirm/sendMsg/{msg}")
    public MallResult confirmSendMsg(@PathVariable String msg) {
        mqDemoProductor.sendConfirmMsg(msg);
        return MallResult.success();
    }

    /**
     * 发布确认-回退模式
     * 模拟到达交换机，分配队列失败的情况
     * */
    @RequestMapping("/return/sendMsg/{msg}")
    public MallResult returnSendMsg(@PathVariable String msg) {
        mqDemoProductor.sendReturnMsg(msg);
        return MallResult.success();
    }

    /**
     * 重试机制
     * 默认无线重试
     * */
    @RequestMapping("/retry/sendMsg/{msg}")
    public MallResult retrySendMsg(@PathVariable String msg) {
        mqDemoProductor.sendRetryMsg(msg);
        return MallResult.success();
    }

    /**
     * 死信队列
     * */
    @RequestMapping("/dead/sendMsg/{msg}")
    public MallResult deadSendMsg(@PathVariable String msg){
        mqDemoProductor.sendDeadMsg(msg);
        return MallResult.success();
    }
}
